/*  
 * Copyright (C) 2011 Marta Spodymek
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 */
package org.prometheuscode.web.feedback.spring.controller;

import java.util.List;

import org.prometheuscode.web.feedback.ApplicationConfig;
import org.prometheuscode.web.feedback.domain.IFeed;
import org.prometheuscode.web.feedback.service.IFeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * This controller handles all "feed" related stuff.
 * 
 * @author marta
 * 
 */
@Controller
@RequestMapping("/user/{user}/*")
public class FeedController {

    private IFeedService feedService;

    private ApplicationConfig appConfig;

    public FeedController() {
    }

    /**
     * @return the appConfig
     */
    public ApplicationConfig getApplicationConfig() {
	return appConfig;
    }

    /**
     * @param appConfig
     *            the appConfig to set
     */
    @Autowired
    public void setApplicationConfig(ApplicationConfig appConfig) {
	this.appConfig = appConfig;
    }

    /*
     * Setters and getters
     */

    @Autowired
    public void setFeedService(IFeedService feedService) {
	this.feedService = feedService;

    }

    public IFeedService getFeedService() {
	return this.feedService;
    }

    /**
     * Get list of feeds.
     * 
     * @param model
     * @param userLogin
     * @return
     */
    @RequestMapping(value = { "/feeds" }, method = RequestMethod.GET)
    public String listFeeds(Model model, @PathVariable("user") String userLogin) {

	/* TODO cache results here */
	List<IFeed> feeds = this.feedService.getUserFeeds(userLogin);

	model.addAttribute("feeds", feeds);

	model.addAttribute(this.appConfig.getContentPageName(),
		this.appConfig.getPagePath("feeds"));
	return this.appConfig.getMainTemplateName();
    }

}
